<template>
  <view>
    <view class="goods-list">
      <view v-for="(item,i) in goodsList" :key='i' @click="gotoDetail(item)">
        <my-goods :goods="item"></my-goods>
      </view>

    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: 0,
        defaultPic: '/static/basicprofile.png',
        falg: true
      }
    },
    methods: {
      async getGoodsList(cb) {
        this.falg = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search?', this.queryObj)
        if (res.meta.status !== 200) {
          return uni.showMsg()
        }
        this.falg = false
        cb && cb() // 等同于 if (cb) {// 判断cb是否有被当成参数传过来，有的话则执行cb函数 cb();}
        console.log(res)
        this.goodsList = [...this.goodsList, ...res.message.goods]
        // this.goodsList=res.message.goods
        this.total = res.message.total
        console.log(this.goodsList)
      },
      gotoDetail(item){
        uni.navigateTo({
          url:'/subpak/godds_detail/godds_detail?good_id='+item.goods_id
        })
      }
    },
    onReachBottom() {
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
        return uni.showMsg("到底啦...")
      }
      if (this.falg) {
        return uni.showMsg('正在加载中...')
      }
      this.queryObj.pagenum += 1
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.total = 0
      this.falg = false
      this.goodsList = []
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
    onLoad(option) {
      this.queryObj.query = option.query || ''
      this.queryObj.cid = option.cid || ''
      // console.log(this.queryObj)
      this.getGoodsList()
    },
  }
</script>

<style lang="scss">

</style>
